package zdream.control.test.foe;

import org.junit.Test;
import zdream.control.level.LevelManagerForTest;
import zdream.control.world.MFoe;
import zdream.control.region.Region;
import zdream.control.region.SimpleRegionBuilder;
import zdream.control.world.MHookType;
import zdream.control.world.buff.HealthModule;

import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.*;

/**
 * @author Zdream
 * @date 2024-01-13
 * @since 0.0.1
 */
public class FoeDestroyCase {
	@Test
	public void testFoeDestroyTwice() {
		Region region = new SimpleRegionBuilder(this.getClass().getSimpleName())
				.withRoomPositions("1")
				.withRoom(1)
						.terrain("#########################")
						.terrain("#########################")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("#########################")
						.terrain("#########################")
						.terrain("#########################")
						.build()
				.defineFoe("foe1", "MM2ShotMan")
				.withFoeSpawnAt(6, 3, "foe1")
				.build();
		System.out.println(region);
		assertEquals(1, region.rooms.length);

		LevelManagerForTest world = LevelManagerForTest.builder().build();
		world.setCurrentRoom(region.rooms[0]);

		assertEquals(1, world.foes.size());
		MFoe foe = world.foes.get(0);

		System.out.println("当一只怪在 tick 时被 destroy 了，后续的 tick 应该不能执行");
		AtomicInteger count = new AtomicInteger();

		foe.registerHook()
				.withType(MHookType.TICK)
				.withPriority(9999999)
				.handle(() -> {
					assertFalse(foe.isDestroyed());
					count.incrementAndGet();
				})
				.buildAndSave();

		world.runOneFrame();
		assertEquals(1, count.get());
		assertFalse(foe.isDestroyed());

		HealthModule module = foe.findOneBuff(HealthModule.CATEGORY);
		assertNotNull(module);
		module.hp = 0;

		world.runOneFrame();
		assertTrue(foe.isDestroyed());
	}
}
